/**
 * create by young (young2f@outlook.com) on 2020/12/3
 */
'use strict';
const base = require('../base/controllerBase');

class TreeController extends base {
  async createNode() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        title: { type: 'object' },
        createUser: { type: 'string' },
        table: { type: 'string' },
        tenantCode: { type: 'string' },
        treeId: { type: 'string' }
      };

      ctx.validate(model, body);
      const result = await service.treeService.createNode(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.log(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async createNodeBatch() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const result = await service.treeService.createNodeBatch(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.log(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async clearNode() {
    const { ctx } = this;
    try {
      const { body } = ctx.request;
      const model = {
        table: { type: 'string' },
        treeId: { type: 'string' },
        tenantCode: { type: 'string' }
      };
      ctx.validate(model, body);
      const { treeId, tenantCode, table } = body;
      const where = { fatherId: treeId, table, tenantCode };
      const result = await ctx.model.Tree.deleteMany(where);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.log(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async deleteNode() {
    const { ctx } = this;
    try {
      const { body } = ctx.request;
      const model = {
        table: { type: 'string' },
        treeId: { type: 'string' },
        tenantCode: { type: 'string' }
      };
      ctx.validate(model, body);
      const { tenantCode, treeId, table } = body;
      const result = await ctx.model.Tree.deleteOne({ tenantCode, treeId, table });
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.log(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async moveNode() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        table: { type: 'string' },
        source: { type: 'string' },
        dist: { type: 'string' },
        tenantCode: { type: 'string' }
      };

      ctx.validate(model, body);

      const result = await service.treeService.moveNode(body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.log(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async copyNode() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        table: { type: 'string' },
        source: { type: 'string' },
        dist: { type: 'string' },
        tenantCode: { type: 'string' }
      };
      ctx.validate(model, body);
      const result = await service.treeService.copyNode(body);

      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.log(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findTree() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' }
      };
      ctx.validate(model, body);
      delete body['isSuRegister'];
      const result = await service.treeService.findTree(body);

      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.log(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findTreesWithAuth() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' }
      };
      ctx.validate(model, body);
      delete body['isSuRegister'];
      const result = await service.treeService.findTree(body);

      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.log(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findNodes() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' }
      };

      ctx.validate(model, body);
      delete body['isSuRegister'];
      const result = await service.treeService.findNodes(body);

      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.log(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findData() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = { tenantCode: { type: 'string' }, treeId: { type: 'string' }, table: { type: 'string' } };

      ctx.validate(model, body);
      const { tenantCode, treeId, table } = body;
      const result = await service.treeService.findData({ tenantCode, treeId, table });

      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.error(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async updateNode() {
    const { ctx } = this;
    try {
      const { body } = ctx.request;
      const model = { tenantCode: { type: 'string' }, treeId: { type: 'string' }, table: { type: 'string' } };
      ctx.validate(model, body);
      const { tenantCode, treeId, table } = body;
      const result = await ctx.model.Tree.update({ tenantCode, treeId, table }, body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.log(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async moveTreeData() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        treeId: { type: 'string' },
        distId: { type: 'string' },
        code: { type: 'string' },
        table: { type: 'string' },
      };
      for (let item of body) {
        ctx.validate(model, item);
      }
      const res = await service.treeService.moveTreeData(body);
      ctx.body = res;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.log(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async setConfigs() {
    const { ctx } = this;
    try {
      const { body } = ctx.request;
      const model = {
        treeId: { type: 'string' },
        configs: { type: 'object' }
      };

      ctx.validate(model, body);
      const { treeId } = body;
      const result = await ctx.model.Tree.updateOne({ treeId }, body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      console.log(ctx.request.body);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }
}

module.exports = TreeController;
